package org.ycx.upms.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ycx.common.annotation.BaseService;
import org.ycx.common.base.BaseServiceImpl;
import org.ycx.common.db.DataSourceEnum;
import org.ycx.common.db.DynamicDataSource;
import org.ycx.upms.dao.mapper.ISecGroupMapper;
import org.ycx.upms.dao.mapper.ISecGroupRoleMapper;
import org.ycx.upms.dao.mapper.ISecUserMapper;
import org.ycx.upms.dao.mapper.ISecUserRoleMapper;
import org.ycx.upms.dao.model.*;
import org.ycx.upms.service.ISecGroupService;

import java.util.ArrayList;
import java.util.List;

/**
 * SecGroupService实现
 * Created by yang on 2017-07-19 15:16:03.
 */
@Service
@Transactional
@BaseService
public class SecGroupServiceImpl extends BaseServiceImpl<ISecGroupMapper, SecGroup, SecGroupExample> implements ISecGroupService {

    private static Logger _log = LoggerFactory.getLogger(SecGroupServiceImpl.class);

    @Autowired
    ISecGroupMapper mapper;

    @Autowired
    ISecGroupRoleMapper groupRoleMapper;

    @Autowired
    ISecUserMapper userMapper;

    @Autowired
    ISecUserRoleMapper userRoleMapper;

    @Override
    public List<Integer> selectRidsByGid(Integer uid) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        SecGroupRoleExample sgre = new SecGroupRoleExample();
        sgre.createCriteria().andGroupIdEqualTo(uid);
        List<SecGroupRole> sgus = this.groupRoleMapper.selectByExample(sgre);
        List<Integer> gids = new ArrayList<Integer>();
        for (SecGroupRole ss : sgus) {
            gids.add(ss.getRoleId());
        }
        return gids;
    }

    @Override
    public int updateGroupRole(Integer gid, Integer[] rids) {
        DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
        int res = 0;
        //更新用户group
        SecGroupRoleExample sgu = new SecGroupRoleExample();
        sgu.createCriteria().andGroupIdEqualTo(gid);
        res += this.groupRoleMapper.deleteByExample(sgu);
        if (rids != null && rids.length > 0) {
            for (Integer rid : rids) {
                SecGroupRole secGroupUser = new SecGroupRole();
                secGroupUser.setRoleId(rid);
                secGroupUser.setGroupId(gid);
                res += this.groupRoleMapper.insert(secGroupUser);
            }
        }
        //更新角色
        res += this.userRoleMapper.deleteByGroup(gid);
        List<SecUser> users = this.userMapper.selectUserByGroup(gid);
        if (users != null && users.size() > 0) {
            for (SecUser user : users) {
                for (Integer role : rids) {
                    SecUserRoleExample sure = new SecUserRoleExample();
                    sure.createCriteria().andUserIdEqualTo(user.getUid()).andRoleIdEqualTo(role);
                    long count = this.userRoleMapper.countByExample(sure);
                    if (count > 0) {
                        continue;
                    } else {
                        res += this.userRoleMapper.insertGroupRole(role, user.getUid(), gid);
                    }
                }
            }
        }
        return res;
    }

    @Override
    public List<SecUser> selectByGroup(Integer gid, int offset, int limit) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        return this.userMapper.selectUserByGroup(gid);
    }

    @Override
    public long countByGroup(Integer gid) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        return this.userMapper.countUserByGroup(gid);
    }
}